#!/usr/bin/env bash

# Allows to binge watch YT videos through IncogTube's onion instance
#
# Basically eliminating the need of running "export http_proxy=... && mpv ..."
# again and again when binge watching videos. Asks to input one URL, plays it.
# When it ends or player is closed manually, asks for another URL to play
# through the onion service again. This will repeat until Ctrl+C is pressed or
# empty input is passed.
#
# IncogTube proxies videos, so data comes from their servers, without users
# having to directly talk to YT servers. IncogTube offers their Invidious
# service over I2P, TOR, Yggdrasil in addition to clearnet. Since this script
# plays videos through their TOR onion service it adds a layer of protection
# over the existing privacy benefits of using Invidious.
#
# WARNING: This has not been extensively tested for security. There is
#   absolutely no warranty. Users are solely responsible for everything
#   related to this script and how it is used.
# Please do not misuse the service.
#
# Instructions:
# 1. Install mpv, tor, privoxy and bash to run this script
# 2. Edit /etc/privoxy/config to add: forward-socks5t / 127.0.0.1:9050 .
#    Remember to change 9050 to your TOR SOCKS port in torrc
# 3. Start tor service or run tor manually (if not already)
# 4. Start privoxy service
# 5. Run this script:
#      bash /path/to/this/yt-tor-binge.sh or ./yt-tor-binge.sh
# 6. Enter a YT URL (works with Invidious sub/domain URLs too)
#
# License: CC0 1.0 Universal

# Privoxy HTTP proxy port.
# Change this only if you've set a different port than the default 8118 on
# Privoxy's config file.
HTTP_PROXY_PORT='8118'

export http_proxy=http://127.0.0.1:${HTTP_PROXY_PORT}
INPUT='--' # Free pass for the first loop

until [ -z "$INPUT" ]; do
	echo '== Enter YT/Invidious URL to play or Ctrl+C to quit:'
	read INPUT
	if [ -n "$INPUT" ]; then
		YT_VIDEO_ID=$( echo "$INPUT" | sed -nr 's|.*v=(.*)|\1|p' )
		echo "-- Video id parsed as: ${YT_VIDEO_ID}"
		echo "-- Launching mpv through IncogTube's .onion instance..."
		mpv http://tuberyps2pn6dor6h47brof3w2asmauahhk4ei42krugybzzzo55klad.onion/watch?v=${YT_VIDEO_ID}
	else
		echo '== Empty Input. Exiting...'
	fi
done
